$(function() {

	if (!window.localStorage) {
		alert("浏览器支持localstorage");
		return false;
	} else {
		var uid = window.localStorage.getItem("user_id");
		var gid = window.sessionStorage.getItem("gid");
		var LFC = window.sessionStorage.getItem("loginFromCar");
		var LFM = window.sessionStorage.getItem("loginFromMyself");
		// 点击返回上一页
		$(".titleTea a").click(function() {
			if (gid != null) {
				window.history.back();
			}else{
				window.location.href="../index.html"
			}

		})
		var $val = "";
		var user_id = "";
		$(".mobile").blur(function(event) {
			$val = $.trim($(".mobile").val());
			if ($val == "") {
				$(".mask").html("请输入手机号码").fadeIn(1000, function() {
					$(".mask").fadeOut(1200);
				});
			} else if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test($val))) {
				$(".mask").html("请输入正确的手机号码").fadeIn(1000, function() {
					$(".mask").fadeOut(1200);
				});
			}
		});
		$(".passWord").blur(function(event) {
			var $passVal = $.trim($(".passWord").val());
			if ($passVal == "") {
				$(".mask").html("请输入密码").fadeIn(1000, function() {
					$(".mask").fadeOut(1200);
				});
			}
		});
		// 从加入购物车跳过来时用的
		if (LFC != null) {
			$(".sub").on("click", function(event) {
				var result = $('#loginForm').serialize();
				$.ajax({
					url: 'http://www.teaweilai.com/app/Admin/Register/AppLogin',
					type: 'POST',
					dataType: 'json',
					data: $('#loginForm').serialize(),
					success: function(backdata) {
						console.log(backdata.sing);
						if (backdata.sing == "0") {
							$(".mask").html("该手机号尚未注册").fadeIn(1000, function() {
								$(".mask").fadeOut(1500);
							})
						} else if (backdata.sing == "error") {
							$(".mask").html("用户名密码不匹配").fadeIn(1000, function() {
								$(".mask").fadeOut(1500);
							})
						} else {
							console.log(backdata);
							user_id = backdata.sing.user_id;
							console.log(user_id);
							$val = $(".mobile").val();
							console.log($val);

							window.localStorage.setItem("user", $val);
							window.localStorage.setItem("user_id", user_id);
							 window.sessionStorage.getItem("LFC");
							window.location.href = "shop_car.html";
						}
					},
					beforeSend: function() {
						if (result == "") {
							$(".mask").html("请输入正确信息").fadeIn(1000, function() {
								$(".mask").fadeOut(1200);
							})
						}
					}
				})
			});
		}
		// 从myself里过来
		if (LFM != null) {
			$(".sub").on("click", function(event) {
				var result = $('#loginForm').serialize();
				$.ajax({
					url: 'http://www.teaweilai.com/app/Admin/Register/AppLogin',
					type: 'POST',
					dataType: 'json',
					data: $('#loginForm').serialize(),
					success: function(backdata) {
						if (backdata.sing == "0") {
							$(".mask").html("该手机号尚未注册").fadeIn(1000, function() {
								$(".mask").fadeOut(1500);
							})
						} else if (backdata.sing == "error") {
							$(".mask").html("用户名密码不匹配").fadeIn(1000, function() {
								$(".mask").fadeOut(1500);
							})
						} else {
							user_id = backdata.sing.user_id;

							window.localStorage.setItem("user_id", user_id);
							window.localStorage.setItem("user", $val);
							 window.sessionStorage.getItem("LFM");

							window.location.href = "myself.html";
						}
					},
					beforeSend: function() {
						if (result == "") {
							$(".mask").html("请输入正确信息").fadeIn(1000, function() {
								$(".mask").fadeOut(1200);
							})
						}
					}
				})
			});
		}
		// 从商品详情过来
		if (gid != null) {
			$(".sub").on("click", function(event) {
				var result = $('#loginForm').serialize();
				$.ajax({
					url: 'http://www.teaweilai.com/app/Admin/Register/AppLogin',
					type: 'POST',
					dataType: 'json',
					data: $('#loginForm').serialize(),
					success: function(backdata) {
						if (backdata.sing == "0") {
							$(".mask").html("该手机号尚未注册").fadeIn(1000, function() {
								$(".mask").fadeOut(1500);
							})
						} else if (backdata.sing == "error") {
							$(".mask").html("用户名密码不匹配").fadeIn(1000, function() {
								$(".mask").fadeOut(1500);
							})
						} else {
							user_id = backdata.sing.user_id;

							window.localStorage.setItem("user_id", user_id);
							window.localStorage.setItem("user", $val);
							window.location.href = "tea_detail.html";
						}
					},
					beforeSend: function() {
						if (result == "") {
							$(".mask").html("请输入正确信息").fadeIn(1000, function() {
								$(".mask").fadeOut(1200);
							})
						}
					}
				})
			});
		}
	}
})